package com.aegean.socket.base;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TraditionalSocketTCPBIO extends Thread {
	private Socket s;

	public TraditionalSocketTCPBIO() {

	}

	public TraditionalSocketTCPBIO(Socket s) {
		this.s = s;
	}

	@Override
	public void run() {
		try {
			server(s);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		System.out.println("程序开始");
		if (args.length > 0) {
			System.out.println("启动server");
			serverStart();
		} else {
			System.out.println("启动client");
			Client client =new Client();
			new Thread(client).start();
		}
	}

	public static void serverStart() throws Exception {
		System.out.println("ServerStart函数运行");
		ServerSocket server = null;
		try {
			server = new ServerSocket(8888);
			while (true) {
				Socket s = server.accept();
				new TraditionalSocketTCPBIO(s).start();
			}

		} catch (Exception e) {
			e.printStackTrace();
		} 

	}

	

	public static void server(Socket sc) throws IOException {
		InputStream in = null;
		OutputStream out = null;
		// OutputStream out = null;
		System.out.println("Server函数运行");
		int len = 0;
		try {
			if (sc == null) {
				System.out.println("没有收到客户端的IP地址和消息");
			} else {
				sc.setReceiveBufferSize(1024);
				in = sc.getInputStream();
				out = sc.getOutputStream();
				System.out.println("进入server1");
				out.write(("欢迎您的消息发送").getBytes());
				byte[] buffer = new byte[1024];
				System.out.println("server:" + in.available());
				while ((len = in.read(buffer, 0, 1024)) != -1) {
					System.out.println("server:" + buffer.length);
					System.out.println("接受客户端:" + sc.getLocalPort() + ":"
							+ sc.getInetAddress() + " 数据:" + buffer.toString());
				}
				System.out.println("进入server2");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.flush();
			out.close();
			in.close();
			sc.close();
		}
	}
}
class Client implements Runnable {
	private InputStream in = null;
	private OutputStream out = null;
	private Socket client = null;
	private int len = 0;

	public Client() {

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("Client函数运行");
		try {
			client = new Socket(InetAddress.getByName(null), 8888);
			in = client.getInputStream();
			out = client.getOutputStream();
			System.out.println("接受消息开始");
			byte[] buffer = new byte[1024];
			System.out.println("client:" + buffer.length);
			while ((len = in.read(buffer, 0, 1024)) != -1) {
				System.out.println("client:" + buffer.length);
				System.out.println("接受服务器," + " 数据:" + buffer.toString());
			}
			System.out.println("接受消息结束");
			out.write(("我已经到达，并且报道" + client.getLocalAddress()).getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.flush();
				out.close();
				in.close();
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}